<?php
//先设置命名空间为后台，控制器
namespace Admin\Controller;
//使用Think文件夹里面的Controller类，并一定要继承controller，才可使自定义控制正常使用

use Think\Controller;
class ActivityController extends CommonController{

    //定义控制器的方法

    /**
     *
     * PC端口旅行攻略管理
     *
     ***/
    function index(){
        $activity = M('pc_lvpai');
        $ac_list = $activity->where('is_show = 1')->order("id desc")->select();
        $this->assign('list',$ac_list);
        $this -> display();
    }

    /**
     *
     * 活动创建
     *
     ***/
    function create(){
        //查询所有的城市
        $j_list = M('base')->where()->field('id,name')->select();
        $this->assign('j_list',$j_list);
        $this -> display();
    }

    /**
     *
     * 新闻更新页面
     * 是通过get方法传递新闻ID的数值
     *
     ***/
    function update(){
        $id  = $_GET['id'];
        $j_list = M('base')->where()->field('id,name')->select();
        $this->assign('j_list',$j_list);
        $lv = M('pc_lvpai');
        $list = $lv->where("id = '$id'")->select();
        $this->assign('list',$list);
        $this -> display();
    }



    /**
     *
     * 新闻更新页面
     * 是通过get方法传递新闻ID的数值
     *
     * **/
    function renew(){
        $id  = $_POST['id'];
        //添加图片
        import('ORG.Net.UploadFile');
        $config = C('config');
        $config_qiniu = C('config_qiniu');
        $upload = new \Think\Upload($config,'Qiniu',$config_qiniu);

        $info = $upload->upload();

        if (!$info) {// 上传错误提示错误信息

        }else{
            if($info['avatar'] and $info['image']){
                $_POST['avatar'] = $info['avatar']['url'];
                $_POST['image'] = $info['image']['url'];
            }elseif(!$info['avatar'] and $info['image']){
                $_POST['image'] = $info['image']['url'];
            }elseif($info['avatar'] and !$info['image']){
                $_POST['avatar'] = $info['avatar']['url'];
            }

        }

        $news = M('pc_lvpai');
        $list = $news->where("id = '$id'")->save($_POST);
        if ($list) {
            $this->success('更新成功', U('index'));
        } else {
            $this->error('更新失败');
        }
    }



    /**
     *
     * 创建活动
     * 新闻是不需要图片的，直接在编辑器里面添加
     * 使用的是和artwe上面添加商品的一样的东西，是使用ajax提交的
     *
     * **/
    function add(){
        //添加图片
        import('ORG.Net.UploadFile');
        $config = C('config');
        $config_qiniu = C('config_qiniu');
        $upload = new \Think\Upload($config,'Qiniu',$config_qiniu);

        $info = $upload->upload();

        if (!$info) {// 上传错误提示错误信息

        }else{
            $_POST['avatar'] = $info['avatar']['url'];
            $_POST['image'] = $info['image']['url'];
        }

        $model = M('pc_lvpai');
        $a = $model->add($_POST);
        if ($a > 0) {
            $this->success('创建成功', U('index'));
        } else {
            $this->error('添加失败');
        }
    }

    /**
     *
     * 删除新闻
     *
     * **/
    function delete()
    {
        if(!IS_AJAX) {
            $this->ajaxReturn(array(
                'info' => '非法的请求方式'
            ));
        }

        $news_id = $_POST['news_id'];
        if (empty($news_id)) {
            $this->ajaxReturn(array(
                'info' => '活动不存在'
            ));
        }

        $news = M('pc_lvpai');
        $result = $news->delete($news_id);
        if ($result > 0) {
            $data = array(
                'info' => 'ok',
            );
        } else {
            $data = array(
                'info' => '删除失败'
            );
        }
        $this->ajaxReturn($data);
    }



    /**
     *
     * 更新新闻
     * 可以更新友情链接名称和src
     *
     * **/

    function save()
    {
        if(!IS_AJAX) {
            $this->ajaxReturn(array(
                'info' => '非法的请求方式'
            ));
        }

        $news_id = $_POST['news_id'];
        if (empty($news_id)) {
            $this->ajaxReturn(array(
                'info' => '新闻不存在'
            ));
        }

        $data['name'] = $_POST['name'];
        $data['content']  = $_POST['content'];
        $data['title']  = $_POST['title'];
        $news = M('news');
        $result = $news->where("news_id = '$news_id'")->save($data);
        if ($result > 0) {
            $data = array(
                'info' => '1',                //更新新闻成功
            );
        } else {
            $data = array(
                'info' => '-1'                //更新新闻失败
            );
        }
        $this->ajaxReturn($data);
    }

}
